define(function (require, exports) {
    var base = require("customize/ui/base");
    var dialog = require("customize/bootstrap/dialog");
    var jsons = require("customize/util/json");

    exports.validElements = function (target) {
        target = base.jq(target);
        return target.find("input[name],select[name],textarea[name]");
    };

    exports.firstValidElement = function (target) {
        return exports.validElements(target).first();
    };

    exports.focusFirstValidElement = function (target) {
        exports.firstValidElement(target).focus();
    };

    exports.toJson = function (target, exclude) {
        target = base.jq(target);

        var json = {};

        exports.validElements(target).each(function () {
            var obj = $(this);
            var type = this.type;

            if (exclude && obj.is(exclude)) {
                return;
            }

            if (obj.is(":disabled")) {
                return;
            }
            if (type == "checkbox") {
                if (obj.hasClass("boolean")) {
                    if (obj.prop("checked")) {
                        obj.val("true");
                    } else {
                        obj.val("false");
                    }
                } else {
                    if (!obj.prop("checked")) {
                        return;
                    }
                }
            }
            if (type == "radio" && !obj.prop("checked")) {
                return;
            }

            var name = obj.attr("name");
            var value = obj.val();

            if (name) {
                json = jsons.setFromPath(json, name, value);
            }
        });

        return json;
    };

    exports.toJsonRequest = function (target, exclude) {
        return {
            body: exports.toJson(target, exclude)
        };
    };

    exports.submit = function (target, api, beforeSubmit, success, error, exclude, waitingMessage) {
        var request = exports.toJsonRequest(target, exclude);

        if (beforeSubmit) {
            request = beforeSubmit(request);
            if (!request) {
                return;
            }
        }

        //dialog.processing(waitingMessage, target);
        $.post(api.indexOf("api") > -1 ? ctx + api : api, {json: $.toJSON(request)},
            function (response) {
                //dialog.processing("close", target);
                exports.focusFirstValidElement(target);
                if (response) {
                    if (response.returnCode == "0") {

                        target = base.jq(target);
                        if (target.data("formTemplate")) {
                            target.html(target.data("formTemplate"));
                        }

                        if (success) {
                            success(response);
                        }
                    } else {
                        if (error) {
                            error(response);
                        }
                    }
                } else {
                    alert("返回体为空");
                }
            }, "json"
        ).error(function () {
                dialog.waiting("close");
                alert("网络连接错误");
            });
    };

    exports.post = function (api, param, beforeSubmit, success, error, waitingMessage) {
        var request = {
            body: param
        };

        if (beforeSubmit) {
            request = beforeSubmit(request);
            if (!request) {
                return;
            }
        }

        dialog.processing(waitingMessage);
        $.post(ctx + api, {json: $.toJSON(request)},
            function (response) {
                dialog.processing("close");
                if (response) {
                    if (response.returnCode == "0") {
                        if (success) {
                            success(response);
                        }
                    } else {
                        if (error) {
                            error(response);
                        }
                    }
                } else {
                    alert("返回体为空");
                }
            }, "json"
        ).error(function () {
                dialog.processing("close");
                alert("网络连接错误");
            });
    };


    exports.fill = function (target, json) {
        target = base.jq(target);

        target.find("input[name],select[name],textarea[name]").andSelf().each(function () {
            var obj = $(this);
            var value = jsons.getFromPath(json, obj.attr("name"));
            if (obj.hasClass("skip-fill")) {
                return;
            }

            if (obj.is(":checkbox") || obj.is(":radio")) {
                if (obj.hasClass("boolean")) {
                    obj.attr("checked", value);
                } else {
                    obj.val(value);
                }
            } else if (obj.hasClass("date")) {
                obj.val(value.toDate().pattern("yyyy-MM-dd"));
            } else {
                obj.val(value);
            }
        });
    };

});